﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Text;
using System.Windows.Forms;

namespace SatisGonderim
{
    public partial class frmMain : Form
    {
        Aktarim aktarim = new Aktarim();
        Mikro mk = new Mikro();

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            notifyIcon1.ShowBalloonTip(1, "Boss Screen", "Uygulama başlatılıyor...", ToolTipIcon.Info);

            timer1.Interval = Cevir.Parse<int>(Sessions.Get[Settings.AktarimSuresi]) * 1000 * 60;
            timer1.Enabled = true;

            dateEdit1.DateTime = DateTime.Now;
            dateEdit2.DateTime = DateTime.Now;
            this.WindowState = FormWindowState.Minimized;

            try
            {
                mk.TableCreate(DateTime.Now);
            }
            catch (Exception)
            {
            }
        }

        private void çıkışToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmLogin log = new frmLogin();
            if (log.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Application.Exit();
            }
        }

        private void geçmişGünSatışınıGönderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmLogin log = new frmLogin();
            if (log.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
                this.WindowState = FormWindowState.Normal;
                this.Width = 333;
                this.Height = 211;
                this.ShowInTaskbar = true;
                this.Show();
            }
        }

        private void frmMain_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.ShowInTaskbar = false;
                this.Hide();
            }
        }

        private void btnTamam_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy)
            {
                notifyIcon1.ShowBalloonTip(10, "Boss Screen", "Lütfen daha sonra tekrar deneyin\r\nŞuan aktarım yapılıyor...", ToolTipIcon.Info);
            }
            else
            {
                timer1.Enabled = false;
                int gun = Convert.ToInt32((dateEdit2.DateTime - dateEdit1.DateTime).TotalDays) + 1;
                try
                {
                    for (int i = 0; i < gun; i++)
                    {
                        mk.TableCreate(dateEdit1.DateTime.AddDays(i));
                        mk.TruncateTable(dateEdit1.DateTime.AddDays(i));
                        aktarim.OncekiGunSatisi(dateEdit1.DateTime.AddDays(i));
                    }
                }
                catch (Exception)
                {
                }
                MessageBox.Show("Tamamlandı !");
                timer1.Enabled = true;
                this.ShowInTaskbar = false;
                this.Hide();
            }
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.ShowInTaskbar = false;
            this.Hide();
            notifyIcon1.ShowBalloonTip(10, "Boss Screen", "Uygulama halen çalışıyor...", ToolTipIcon.Info);
        }

        private void ayarlarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmLogin log = new frmLogin();
            if (log.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                frmAyarlar ayar = new frmAyarlar();
                if (ayar.ShowDialog() == DialogResult.OK)
                {
                    Application.Restart();
                }
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                aktarim.Start(DateTime.Now);
            }
            catch (Exception)
            {

            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                if (!backgroundWorker1.IsBusy)
                {
                    timer1.Enabled = false;
                    backgroundWorker1.RunWorkerAsync();
                }
            }
            catch (Exception)
            {
            }
        }

        bool kapat = false;
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            timer1.Enabled = true;
            if (DateTime.Now.Hour == 22 && DateTime.Now.Minute == 30)
            {
                foreach (var item in Process.GetProcesses())
                {
                    if (item.ProcessName.ToLower().IndexOf("genius") > -1)
                    {
                        item.Kill();
                    }
                }

                Process.Start("shutdown.exe", "-s -f -t 00");
                kapat = true;
            }

            if (kapat)
            {
                Shutdown();
            }
        }

        void Shutdown()
        {
            foreach (var item in Process.GetProcesses())
            {
                if (item.ProcessName.ToLower().IndexOf("genius") > -1)
                {
                    item.Kill();
                }
            }

            ManagementBaseObject mboShutdown = null;
            ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
            mcWin32.Get();

            // You can't shutdown without security privileges
            mcWin32.Scope.Options.EnablePrivileges = true;
            ManagementBaseObject mboShutdownParams =
                     mcWin32.GetMethodParameters("Win32Shutdown");

            // Flag 1 means we want to shut down the system. Use "2" to reboot.
            mboShutdownParams["Flags"] = "1";
            mboShutdownParams["Reserved"] = "0";
            foreach (ManagementObject manObj in mcWin32.GetInstances())
            {
                mboShutdown = manObj.InvokeMethod("Win32Shutdown",
                                               mboShutdownParams, null);
            }

            Process.Start("shutdown.exe", "-s -f -t 00");
        }

        private void günsonuİşlemiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                decimal local = aktarim.GunlukCiro(DateTime.Now);
                decimal uzak = mk.GunlukCiro(DateTime.Now);
                decimal fark = 0;
                if (uzak > local)
                {
                    fark = uzak - local;
                }
                else
                {
                    fark = local - uzak;
                }
                if (fark > 10)
                {
                    timer1.Enabled = false;
                    try
                    {
                        mk.TruncateTable(DateTime.Now);
                        aktarim.Start(DateTime.Now);
                    }
                    catch (Exception)
                    {
                    }
                    timer1.Enabled = true;
                    MessageBox.Show("Satış kontrol edildi !");
                }
            }
            catch (Exception)
            {
            }
        }
    }
}
